package cn.wolfcode.car.controller;

import cn.wolfcode.car.domain.Business;
import cn.wolfcode.car.domain.Employee;
import cn.wolfcode.car.domain.Salary;
import cn.wolfcode.car.domain.SystemDictionaryItem;
import cn.wolfcode.car.enumpkg.ConsumptionStatusEnum;
import cn.wolfcode.car.qo.SalaryDetailQueryObject;
import cn.wolfcode.car.qo.SalaryQueryObject;
import cn.wolfcode.car.service.IEmployeeService;
import cn.wolfcode.car.service.ISalaryService;
import cn.wolfcode.car.service.ISystemDictionaryItemService;
import cn.wolfcode.car.vo.BusinessReportVO;
import cn.wolfcode.car.vo.ChartVO;
import cn.wolfcode.car.vo.SalaryReprotVo;
import cn.wolfcode.car.vo.SalaryVO;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import result.JsonResult;

import java.util.List;

@Controller
@RequestMapping("/salary")
public class SalaryController {
    @Autowired
    private ISalaryService salaryService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private IEmployeeService employeeService;


    //@RequiresPermissions(value = {"salary:list", "部门页面"}, logical = Logical.OR)
    //@RequiredPermission(name = "部门页面", expression = "salary:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SalaryQueryObject qo) {
        PageInfo<Salary> list = salaryService.list(qo);
        model.addAttribute("result", list);
        //方法方式
        List<SystemDictionaryItem> type = systemDictionaryItemService.getByType(4L);
        model.addAttribute("type", type);
        //员工
        List<Employee> employees = employeeService.listAll();
        model.addAttribute("employees", employees);
        return "salary/list";
    }

    @RequiresPermissions(value = {"salary:saveOrUpdate", "部门修改"}, logical = Logical.OR)
    @PostMapping("/saveOrUpdate")
    public String saveOrUpdate(Salary salary) {
        if (salary.getId() == null) {
            salaryService.save(salary);
        } else {
            salaryService.update(salary);
        }
        return "redirect:/salary/list";
    }

    @RequestMapping("/detail")
    public String detail(Model model, @ModelAttribute("qo") SalaryDetailQueryObject qo) {
        PageInfo<SalaryReprotVo> list = salaryService.selectByGroup(qo);
        model.addAttribute("result", list);
        System.out.println(list);
        //方法方式
        List<SystemDictionaryItem> type = systemDictionaryItemService.getByType(4L);
        model.addAttribute("type", type);

        List<SalaryReprotVo> all = salaryService.selectByGroupNotLimit(qo);

        SalaryVO salaryVO = new SalaryVO();
        for (int i = 0; i < all.size(); i++) {
            SalaryReprotVo salaryReprotVo = all.get(i);
            salaryVO.getCond().add(salaryReprotVo.getCond());
            salaryVO.getTotal().add(salaryReprotVo.getTotal());
            salaryVO.getAverage().add(salaryReprotVo.getAverage());
        }
        System.out.println(JSON.toJSONString(salaryVO));
        model.addAttribute("chart", salaryVO);

        return "salaryReport/list";
    }

}
